package com.syzg.view.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.syzg.common.model.WebObj;
import com.syzg.core.page.DefaultPageResult;
import com.syzg.core.page.PageResult;
import com.syzg.core.util.StringUtil;

public class PagerTag extends SimpleTagSupport {
	
	private String id;
	private String value;
    private String cssClass;
    private String cssStyle;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}
	
	public void setValue(String value) {
		this.value = value;
	}

	public String getCssClass() {
		return cssClass;
	}
	
	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssStyle() {
		return cssStyle;
	}

	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}

	@Override
	public void doTag() throws JspException, IOException {
		PageContext pageContext = (PageContext)getJspContext();
		JspWriter out = this.getJspContext().getOut();
		StringBuffer sb = new StringBuffer();
		WebObj webObj = (WebObj)pageContext.getRequest().getAttribute("webObj");
		if(webObj == null){
			webObj = (WebObj)pageContext.getRequest().getAttribute(value);
		}
		PageResult pageResult = webObj.getPageResult();
		DefaultPageResult depager = (DefaultPageResult)pageResult;
		
		if(StringUtil.isNotEmpty(cssClass)){
			sb.append("﻿<div class=\"" + cssClass + "\"");
		} else {
			sb.append("﻿<div class=\"pageDown\"");
		}
		if(StringUtil.isNotEmpty(cssStyle)){
			sb.append(" style=\"" + cssStyle + "\"");
		}
		sb.append(">");
		sb.append(String.format("﻿<span class=\"pageDown_sum\">共 %s 条记录 当前第 %s 页/共 %s 页</span>", depager.getTotalCount(),depager.getCurrentPageNo(),depager.getTotalPageCount()));
		
		sb.append("&nbsp;&nbsp;");
		
		//首页
		sb.append("<span class=\"pageDown_nav\">");
		if(depager.getCurrentPageNo()>1){
			sb.append("<a href=\"javascript:gotoPage(1);\">首页</a>");
		} else {
			sb.append("首页");
		}
		sb.append("</span>  ");
		
		//上一页
		sb.append("<span class=\"pageDown_nav\">");
		if(depager.getCurrentPageNo()>1){
			sb.append(String.format("<a href=\"javascript:gotoPage(%s);\">上一页</a>", depager.getCurrentPageNo()-1));
		} else {
			sb.append("上一页");
		}
		sb.append("</span>  ");
		
		//下一页
		sb.append("<span class=\"pageDown_nav\">");
		if(depager.getCurrentPageNo()<depager.getTotalPageCount()){
			sb.append(String.format("<a href=\"javascript:gotoPage(%s);\">下一页</a>", depager.getCurrentPageNo()+1));
		} else {
			sb.append("下一页");
		}
		sb.append("</span>  ");
		
		//尾页
		sb.append("<span class=\"pageDown_nav\">");
		if(depager.getCurrentPageNo()<depager.getTotalPageCount()){
			sb.append(String.format("<a href=\"javascript:gotoPage(%s);\">尾页</a>", depager.getTotalPageCount()));
		} else {
			sb.append("尾页");
		}
		sb.append(String.format("<input type=\"hidden\" id=\"pageNo\" name=\"pageNo\" value=\"%s\"/>", depager.getCurrentPageNo()));
		sb.append("</span>");
		sb.append("  ");
		sb.append("﻿</div");
		out.println(sb.toString());
	}
}
